using UnityEngine;

/// <summary>
/// 障碍物生成器：负责生成和管理障碍物
/// </summary>
public class ObstacleSpawner : MonoBehaviour
{
    [Header("障碍物设置")]
    [Tooltip("障碍物预制体")]
    [SerializeField] private GameObject obstaclePrefab;
    
    [Header("生成设置")]
    [Tooltip("初始延迟时间")]
    [SerializeField] private float startDelay = 2.0f;
    [Tooltip("生成间隔时间")]
    [SerializeField] private float repeatRate = 2.0f;
    [Tooltip("生成位置")]
    [SerializeField] private Vector3 spawnPos = new Vector3(25, 0, 0);

    private bool isSpawning = true;
    private PlayerController playerController;

    void Start()
    {
        // 获取玩家控制器引用
        playerController = GameObject.Find("Player").GetComponent<PlayerController>();
        
        // 开始定时生成障碍物
        if (obstaclePrefab != null)
        {
            InvokeRepeating(nameof(SpawnObstacle), startDelay, repeatRate);
        }
        else
        {
            Debug.LogError("障碍物预制体未设置！");
        }
    }

    /// <summary>
    /// 生成障碍物
    /// </summary>
    private void SpawnObstacle()
    {
        // 检查游戏是否结束
        if (playerController != null && !playerController.IsGameOver() && isSpawning)
        {
            // 在指定位置生成障碍物
            Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation);
        }
    }

    /// <summary>
    /// 停止生成障碍物
    /// </summary>
    public void StopSpawning()
    {
        isSpawning = false;
    }

    /// <summary>
    /// 重新开始生成障碍物
    /// </summary>
    public void StartSpawning()
    {
        isSpawning = true;
    }
} 